using System;
using System.Data;
using System.Data.Common;
using System.Configuration;

namespace ApplicationSupport
{
	/// <summary>
	/// Configuration tools
	/// </summary>
	public static class Configuration
	{
		#region Public Methods
		/// <summary>
		/// Retrieves a configuration as Boolean
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static bool GetBoolean(string configKey)
		{
			return SafeConvert.ToBoolean(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Byte
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static byte GetByte(string configKey)
		{
            return SafeConvert.ToByte(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Int16
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static short GetInt16(string configKey)
		{
			return SafeConvert.ToInt16(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Int32
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static int GetInt32(string configKey)
		{
			return SafeConvert.ToInt32(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Int64
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static long GetInt64(string configKey)
		{
			return SafeConvert.ToInt64(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Float
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static float GetFloat(string configKey)
		{
			return SafeConvert.ToFloat(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Double
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static double GetDouble(string configKey)
		{
			return SafeConvert.ToDouble(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as Decimal
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static decimal GetDecimal(string configKey)
		{
			return SafeConvert.ToDecimal(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as DateTime
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static DateTime GetDateTime(string configKey)
		{
			return SafeConvert.ToDateTime(GetString(configKey));
		}

		/// <summary>
		/// Retrieves a configuration as String
		/// </summary>
		/// <param name="configKey">The config key</param>
		public static string GetString(string configKey)
		{
			string output = GetStringWeb(configKey);
			if (output == "" && AllowDataBaseSettings)
			{
				try
				{
					output = GetStringDataBase(configKey);
				}
				catch{}
			}
			return output;
		}
		#endregion
		
		#region Private Methods
		private static string GetStringWeb(string configKey)
		{
			return ConfigurationManager.AppSettings[configKey];
		}
		private static string GetStringDataBase(string configKey)
		{
			//return DataTools.GetScalar("EXEC GetConfiguration ?", 0, configKey).ToString();
			throw new NotImplementedException();
		}
		private static bool AllowDataBaseSettings
		{
			get
			{
				return SafeConvert.ToBoolean(GetStringWeb("AllowDataBaseSettings"));
			}
		}
		#endregion
	}
}